package com.aiden.cashbook.widget

import `in`.srain.cube.views.ptr.PtrFrameLayout
import `in`.srain.cube.views.ptr.PtrUIHandler
import `in`.srain.cube.views.ptr.indicator.PtrIndicator
import android.content.Context
import android.view.LayoutInflater
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.FrameLayout
import com.aiden.cashbook.R
import kotlinx.android.synthetic.main.layout_load_header.view.*

class CustomRefreshHeader(context: Context?) : FrameLayout(context), PtrUIHandler {

    private var mRotate: Animation
    private var mBackgroundTranslate1: Animation
    private var mBackgroundTranslate2: Animation
    private var mRiderTranslate: Animation
    private var mRotateSun: Animation

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_load_header, this)
        mRotate = AnimationUtils.loadAnimation(context, R.anim.anim_rotate_wheel)
        mBackgroundTranslate1 = AnimationUtils.loadAnimation(context, R.anim.anim_tranlate_load_bg1)
        mBackgroundTranslate2 = AnimationUtils.loadAnimation(context, R.anim.anim_tranlate_load_bg2)
        mRiderTranslate = AnimationUtils.loadAnimation(context, R.anim.anim_translate_rider)
        mRotateSun = AnimationUtils.loadAnimation(context, R.anim.anim_rotate_sun)
    }
    override fun onUIRefreshComplete(frame: PtrFrameLayout?) {
        stopAnimation()
    }

    override fun onUIPositionChange(frame: PtrFrameLayout?, isUnderTouch: Boolean, status: Byte, ptrIndicator: PtrIndicator?) {

    }

    override fun onUIRefreshBegin(frame: PtrFrameLayout?) {
        startAnimation()
    }

    override fun onUIRefreshPrepare(frame: PtrFrameLayout?) {
    }

    override fun onUIReset(frame: PtrFrameLayout?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    private fun startAnimation() {
        iv_background1.startAnimation(mBackgroundTranslate1)
        iv_background2.startAnimation(mBackgroundTranslate2)
        iv_rider.startAnimation(mRiderTranslate)
        iv_wheel1.startAnimation(mRotate)
        iv_wheel2.startAnimation(mRotate)
        iv_sun.startAnimation(mRotateSun)
    }

    private fun stopAnimation() {
        iv_background1.clearAnimation()
        iv_background2.clearAnimation()
        iv_rider.clearAnimation()
        iv_wheel1.clearAnimation()
        iv_wheel2.clearAnimation()
        iv_sun.clearAnimation()
    }
}